const fp = require('lodash/fp');

const cars = [
	{name: 'Fe FF', horsepower: 660, dollar_value: 700000, in_stock: true},
	{name: 'Sp Cl2', horsepower: 650, dollar_value: 648000, in_stock: false},
	{name: 'Ja XK', horsepower: 550, dollar_value: 132000, in_stock: false},
	{name: 'Audi R8', horsepower: 525, dollar_value: 114200, in_stock: false},
	{name: 'Aston M7', horsepower: 750, dollar_value: 1850000, in_stock: true},
	{name: 'Pai Huay', horsepower: 700, dollar_value: 1300000, in_stock: false},
];

// 取最后一条数据
const isLastInStock = fp.flowRight(fp.prop('in_stock'), fp.last);

console.log(isLastInStock(cars));

//第一个car的Name
const getFirstCarName = fp.flowRight(fp.prop('name'), fp.first);

console.log(getFirstCarName(cars));

const _average = function (xs) {
	return fp.reduce(fp.add, 0, xs) / xs.length;
};

// 计算平均价格 先取每辆车的价格 再取平均
const averageDollarValue = fp.flowRight(
	_average,
	fp.map((car) => car.dollar_value),
);

console.log(averageDollarValue(cars));

const _underscore = fp.replace(/\W+/g, '_');

console.log(_underscore('Hello World'))

// 每个字符小写 再连字符
const sanitizeNames = fp.flowRight(fp.map(fp.flowRight(_underscore, fp.toLower)));

console.log(sanitizeNames(['Hello World']));

// 把cars里的name小写+连字符
const newCars = fp.flowRight(
	sanitizeNames,
	fp.map((car) => car.name),
)

console.log(newCars(cars))
